<?php
class ResumeModel extends Model {
	public function __construct() {
		$this->open(Model::MYSQL);
			$this->calledClass(get_class($this));
	}
	/*Mr. Phireak*/
	public function getValue($fieldname,$where=array()){
		$this->open(Model::MYSQL);
		return parent::getFieldValue("Resumes",$fieldname,$where);
	}
	
	public function display() {
		$this -> open(Model::MYSQL);
		return $this -> getRecords($fields);
	}
	public function edit($fields=array(),$where=array()){
		$this->open(Model::MYSQL);
		return parent::update($fields,$where);
	}
	
	public function Save($fields = array()) {
		$this -> open(Model::MYSQL);
		return parent::save($fields);
	}
	public function getObject($where=array()){
		$this -> open(Model::MYSQL);
		return parent::getAsObject($where);
	}
	public function findAsObject($where=array()){
		$this->open(Model::MYSQL);
		return parent::findAsObject($where);
	}
	public function checkResume($user_id) {
		$this -> open(Model::MYSQL);
		$result = parent::query("select * from Resumes where can_id=$user_id");
		return mysql_num_rows($result) ? true : false;
	}
       public function checkEmailReference($email) {
		$this -> open(Model::MYSQL);
		$query = "SELECT email from Candidates WHERE email ='" . $email . "'";
		$result = parent::query($query);
		$rows = array();
		$row = mysql_fetch_object($result);
		return ($row)?$row->email:NULL;
	}
	public function checkResumeNoYes($user_id) {
		$this -> open(Model::MYSQL);
		$query = "SELECT Resumes.rem_attach FROM Candidates
				INNER JOIN Resumes ON Candidates.id = Resumes.can_id
				INNER JOIN Galleries ON Galleries.id = Resumes.rem_attach
				WHERE Resumes.can_id =" . $user_id;
		$result = parent::query($query);
		$rows = array();
		$row = mysql_fetch_object($result);
		return $row -> rem_attach;
	}

	public function viewCV($user_id) {
		$this -> open(Model::MYSQL);
		$query = "SELECT * FROM Resumes WHERE can_id = $user_id";
		$result = parent::query($query);
		$row = mysql_fetch_object($result);
		if(is_object($row)){
			$query = "SELECT * FROM Candidates WHERE id = $row->can_id ";
			$result = parent::query($query);
			$row->can_id = mysql_fetch_object($result);
		}
		return $row;
	}

	public function getresumeedit($rem_id) {
		$this -> open(Model::MYSQL);
		$query = "SELECT Jobs.* FROM Jobs WHERE Jobs.id=$rem_id";
		$result = parent::query($query);
		$records = array();
		while ($row = mysql_fetch_object($result)) {
			$query = "SELECT Galleries.* FROM Galleries WHERE Galleries.id in ($row->img_id) ;";
			$result1 = $this -> query($query);
			while ($row1 = mysql_fetch_object($result1))
				$row -> img_id = $row1;
			$records = $row;
		}
	}

	/*
	 public function query($id){
	 $this->open(Model::MYSQL);
	 $query = "SELECT * FROM Resumes where id='".$id."'";
	 $query = parent::query($query);
	 while ($row = mysql_fetch_array($query)) {
	 $result[]=$row;
	 }
	 return $result;
	 }
	 */
	/* public function queryAll(){
	 $this->open(Model::MYSQL);
	 $query = "SELECT * FROM Resumes where can_id='".$can_id."'";
	 $query = parent::query($query);
	 while ($row = mysql_fetch_array($query)) {
	 $result[]=$row;
	 }
	 return $result;
	 } */

	public function getcan($ids) {
		$query = "SELECT can_id  FROM Resumes WHERE id='$ids' ;";
		$result = parent::query($query);
		while ($record = mysql_fetch_object($result))
			$records = $record;
		return $records -> can_id;
	}

	public function findArray($where = array()) {
		$this -> open(Model::MYSQL);
		return parent::findAsArray($where);
	}

	public function getResumeWithUser($cid = false) {
		$this -> open(Model::MYSQL);
		$data = array();
		if ($cid != false && is_numeric($cid)) {
			$query = "SELECT * FROM Resumes Where can_id=$cid ;";
			$result = $this -> query($query);
			while ($row = mysql_fetch_object($result)) {
				if ($row -> rem_uploadImg != "") {
					$query = "SELECT * FROM Galleries WHERE Galleries.id in ($row->rem_uploadImg) ";
					$result1 = $this -> query($query);
					while ($row1 = mysql_fetch_Object($result1))$row -> rem_uploadImg = $row1;
				}
				if($row->rem_attach!=""){
					$query = "SELECT * FROM Galleries WHERE Galleries.id in ($row->rem_attach) "; 
					$result1 = $this -> query($query);
					while ($row1 = mysql_fetch_Object($result1))$row -> rem_attach = $row1;
				}
				$data[] = $row;
			}
		}
		return !empty($data) ? $data[0] : array();
	}

	public function getResumeWithId($id = false) {
		$this -> open(Model::MYSQL);
		$data = array();
		if ($id != false && is_numeric($id)) {
			$query = "SELECT * FROM Resumes Where id=$id ;";
			$result = $this -> query($query);
			while ($row = mysql_fetch_object($result)) {
				if ($row -> rem_uploadImg != "") {
					$query = "SELECT * FROM Galleries WHERE Galleries.id in ($row->rem_uploadImg) ";
					$result1 = $this -> query($query);
					while ($row1 = mysql_fetch_Object($result1))
						$row -> rem_uploadImg = $row1;
				}
				$data[] = $row;
			}
		}
		return !empty($data) ? $data[0] : array();
	}

	public function version($table = "", $field = array("id"), $where = array()) {
		$this -> open(Model::MYSQL);
		return parent::getFieldValue($table, $field, $where);
	}

	public function SelectQu($id) {
		$this -> open(Model::MYSQL);
		$datas = array();
		$result = parent::query("select id,name,parent_id from Categories where parent_id=$id");
		while ($row = mysql_fetch_object($result)) {
			$datas[] = $row;
		}
		return $datas;
	}
	public function ExYear($where){
			$this->open(Model::MYSQL);
			$query = "select id,name,parent_id from Categories where parent_id='" .$where. "' ORDER By name ASC";
			$result = parent::query($query);
			$data=array();
			while ($row = mysql_fetch_object($result)) {
				$data[$row->id]= $row;
			}
			return $data;
		}
	public function DisplayAdmin($query, $page = 1, $perpage = 20, $search = array(), $sortFields = array(), $sortCriteria = "ASC", $operation = "AND") {
		$this -> open(Model::MYSQL);
		$page = $page < 1 ? 1 : $page;
		$perpage = $perpage < 1 ? 20 : $perpage;
		$sortCriteria = $sortCriteria === "" ? "ASC" : $sortCriteria;
		$operation = $operation === "" ? "AND" : $operation;
		$query = "$query WHERE";
		if (isset($search) && $search !== "") {
			$tempSearchFields = array();
			foreach ($search as $key => $val) {
				$tempSearchFields[] = " `$key` LIKE '%" . $val . "%' ";
			}
			$query .= implode(" $operation ", $tempSearchFields);
		}
		if (!empty($sortFields)) {
			$tempSortFields = array();
			foreach ($sortFields AS $field) {
				$tempSortFields[] = " `$field` ";
			}
			$query .= " ORDER BY " . implode(" , ", $tempSortFields) . " " . $sortCriteria . " ";
		}
		$totalRecords = mysql_num_rows(parent::query($query));
		$totalPages = ceil($totalRecords / $perpage);
		$query .= " LIMIT " . (($page - 1) * $perpage) . " , " . $perpage . " ; ";
		$result = parent::query($query);
		if ($result !== null) {
			$rows = $result;
			$result = array();
			while (($row = mysql_fetch_object($rows)) !== false)
				$result[] = $row;
		}
		return array("query" => $query, "search" => $search, "page" => $page, "perpage" => $perpage, "totalpages" => $totalPages, "totalrecords" => $totalRecords, "records" => ($result === null ? array() : $result));
	}

	public function getList($page = 1, $perpage = 20, $search = "so", $searchFields = array(), $sortFields = array(), $sortCriteria = "", $wheres=array()) {
		$this -> open(Model::MYSQL);
		
		$page=$page<1?1:$page;
		$perpage=$perpage<1?20:$perpage;
		$sortCriteria=$sortCriteria===""?"ASC":$sortCriteria;
		$query = " SELECT Resumes.*, Candidates.firstname, Candidates.lastname, Candidates.nickname, Candidates.username FROM Resumes,Candidates WHERE Resumes.can_id=Candidates.id AND rem_status = 'ACT'" ;
		
		if(is_array($wheres)&&!empty($wheres)){
				$tempWhereFields=array();
				foreach($wheres AS $field => $val  ){
					if(!empty($val)){
						$tempWhereFields[]= $field ."'".mysql_real_escape_string($val)."'"  ;
					}
				}
				if(!empty($tempWhereFields)){
					$query .= " AND ";
				}
				$query .= implode(" AND ",$tempWhereFields);
			}
			
			if(!empty($searchFields)&&(isset($search)&&$search!=="")){
				$tempSearchFields=array();
				foreach($searchFields AS $field ){
					$tempSearchFields[]= " `$field` LIKE '%".mysql_real_escape_string($search)."%' " ;
				}
				$query .= " AND ( " . implode(" OR ",$tempSearchFields) . " ) ";
			}
		if(!empty($sortFields)){
			$tempSortFields=array();
			foreach($sortFields AS $field ){
				$tempSortFields[]= " $field " ;
			}
			$query .= " ORDER BY " . implode(" , ",$tempSortFields) . " " . $sortCriteria . " " ;
		}
		
		$totalRecords = mysql_num_rows($this->query($query));
		$totalPages = ceil($totalRecords/$perpage);
		$query .= " LIMIT ".(($page-1)*$perpage)." , ".$perpage." ; " ;
		$result = $this->query($query);
		if($result!==null){
			$rows = $result ;
			$result = array();
			while(($row=mysql_fetch_object($rows))!==false)$result[]=$row;
		}
		return array("search"=>$search,"page"=>$page,"perpage"=>$perpage,"totalpages"=>$totalPages,"totalrecords"=>$totalRecords,"records"=>($result===null?array():$result)) ;
		
		
	}
	public function getOverviewResume( $where=array() ){
		$this->open(Model::MYSQL);
		$query = " Select * From Candidates
					INNER JOIN Resumes ON Candidates.id = Resumes.can_id
					INNER JOIN Gallerys ON Candidates.id = Gallerys.uid  ";
		if(is_array($where)&&!empty($where)){
			$tempSearchFields=array();
			foreach($where as $key => $val ){
				if($val!=""&&$val!=NULL)$tempSearchFields[]= " $key'$val' " ;
			}
			$query .= !empty($tempSearchFields)?" WHERE " .  implode(" OR ",$tempSearchFields):"";
		}
		$result = parent::query($query);
		$row=mysql_fetch_object($result);
		return $row;
	}
	
	/**
	 * Modify: Mr.chamnan
	 * date: 2014-03-26
	 */
	
	public function checkCategory($cat_name) {
		$this -> open(Model::MYSQL);
		$query = "select id from Categories where name='".mysql_real_escape_string($cat_name)."'";
		$result = parent::query($query);
		return mysql_num_rows($result) ? true : false;
	}
	public function getCategoryId($cat_name){
			$this -> open(Model::MYSQL);
			$query = "select id from Categories where name='".mysql_real_escape_string($cat_name)."'";
			$row = array();
			$result = parent::query($query);
			$row = mysql_fetch_object($result);
			return $row->id;
		
	}
	public function getSearchSet($id){
		$this -> open(Model::MYSQL);
		$query = "select search_set from Categories where id=".$id;
		$row = array();
		$result = parent::query($query);
		$row = mysql_fetch_object($result);
		return $row;
	
	}
	
	//Desplay resume in a day roth
	public function getResumedisplay($page=1,$perpage=20,$start_date="",$end_date=""){
		$this->open(Model::MYSQL);
		$page=$page<1?1:$page;
		$perpage=$perpage<1?20:$perpage;
		$query ="SELECT *  FROM Resumes";
		$query.=" Where DATE_FORMAT(rem_createDate,'%Y-%m-%d') = '".date("Y-m-d")."' AND rem_name<>'' AND  rem_DOB<>'' AND rem_email<>'' AND  rem_tel<>'' " ;
		$query.=" ORDER BY rem_createDate DESC ";
		$totalRecords = mysql_num_rows(parent::query($query));
		$totalPages = ceil($totalRecords/$perpage);
		$query .= " LIMIT ".(($page-1)*$perpage)." , ".$perpage." ; " ;
		$result = parent::query($query);
		if($result!==null){
			$rows = $result ;
			$result = array();
			while(($row=mysql_fetch_object($rows))!==false)$result[]=$row;
		}
		return array("query"=>$query,"page"=>$page,"perpage"=>$perpage,"totalpages"=>$totalPages,"totalrecords"=>$totalRecords,"records"=>($result===null?array():$result)) ;
	}	
}
?>